//app.js
App({
  onLaunch: function() {
    this.login()
  },
  check: function(cb) {
    var that = this;
    
      wx.getSetting({
        success: function(res) {          
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称
            wx.getUserInfo({
              success: function(res) {
                that.login();
              }
            });
          } else {
            wx.navigateTo({
              url: '/pages/login/index',
            })
          }
        }
      });
    
  },
  //登录
  login: function() {    
    var that = this;    
    //调用登录接口
    wx.login({
      success: function(res) {        
        if (res.code) {
          //发起网络请求
          wx.getUserInfo({
            success: (ures) => {
              wx.request({
                url: 'https://sng.tsletu.com/api/wxapp/public/login',
                method: 'get',
                data: {
                  code: res.code,
                  encrypted_data: ures.encryptedData,
                  iv: ures.iv,
                  signature: ures.signature,
                  raw_data: ures.rawData
                },
                header: {
                  'XX-Device-Type': 'wxapp',
                  'XX-Token': 'xxx'
                },
                success: (res) => {                             
                  that.globalData.token = res.data.data.token;   
                  that.globalData.userInfo = res.data.data.user;                  
                  wx.navigateBack({
                  });
                }
              })
            }
          })
        }
      }
    });
  },
  globalData: {
    userInfo: null,
    token: null
  }
})